package com.youlu.campus.service.visitor.impl;

import com.youlu.campus.entity.UserActivitySubWechatTemplateMsg;
import com.youlu.campus.service.visitor.UserActivitySubWechatTemplateMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class UserActivitySubWechatTemplateMsgServiceImpl implements UserActivitySubWechatTemplateMsgService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void create(UserActivitySubWechatTemplateMsg req) {
        req.setId(null);
        req.setCreatedTime(new Date());
        req=mongoTemplate.insert(req);
        log.info(":>>> 创建用户订阅模版消息结果为:{}",req);
    }

    @Override
    public UserActivitySubWechatTemplateMsg find(String userId, String activityId, String platform, String appId, String templateId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId).
                and("appId").is(appId).and("templateId").is(templateId));
        return mongoTemplate.findOne(query, UserActivitySubWechatTemplateMsg.class);
    }

    @Override
    public boolean exist(String userId, String activityId, String platform, String appId, String templateId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId).
                and("appId").is(appId).and("templateId").is(templateId));
        return mongoTemplate.exists(query, UserActivitySubWechatTemplateMsg.class);
    }
}
